package com.noble.meether.chat.ui.agora.act.dialog.report;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.noble.glamour.module.logic.application.App;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.baseui.LoadingDialog;
import com.noble.glamour.module.logic.config.UrlParam;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.library.log.PToast;
import com.noble.library.observe.MsgMgr;
import com.noble.library.view.base.BaseDialogFragment;
import com.noble.library.widget.RecyclerView.base.BaseRecyclerViewHolder;
import com.noble.meether.R;
import com.noble.meether.chat.ui.agora.act.bean.RtcComplainReasons;
import com.noble.meether.chat.ui.agora.util.RtcSubUtil;
import com.noble.meether.logic.media.UpLoadResult;

/**
 * 视频中投诉弹框
 */
public class RtcVideoCommentDlg extends BaseDialogFragment implements View.OnClickListener, RequestComplete, BaseRecyclerViewHolder.OnItemClickListener {
    private RecyclerView recyclerView;
    private ReportAdapter reportAdapter;

    private int selectedPosition = 0;
    private RtcComplainReasons complainReasons;


    public RtcVideoCommentDlg() {
        settWindowAnimations(R.style.AnimDownInDownOutOverShoot);
        setGravity(Gravity.CENTER);
        setDialogSizeRatio(0, 0);
        setCanceledOnTouchOutside(false);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        setContentView(R.layout.f1_chat_video_comment_dialog);
        initView();
        return getContentView();
    }

    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.addItemDecoration(new ItemSpaces());
        reportAdapter = new ReportAdapter();
        recyclerView.setAdapter(reportAdapter);
        reportAdapter.setOnItemClickListener(this);
        findViewById(R.id.btn_submit).setOnClickListener(this);
        findViewById(R.id.btn_close).setOnClickListener(this);
        complainReasons = ModuleMgr.getCommonMgr().getComplainReasons();
        if (complainReasons == null || complainReasons.getReport_reasons().size() <= 0) {
            ModuleMgr.getCommonMgr().reqGetComplainReasons(this);
            return;
        }
        reportAdapter.setList(complainReasons.getReport_reasons());
    }

    @Override
    public void onItemClick(View convertView, int position) {
        this.selectedPosition = position;
        reportAdapter.setSelected(position);
        reportAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_submit:
                report();
                break;
            case R.id.btn_close:
                dismiss();
                break;
        }
    }

    private void report() {
        if (complainReasons == null || complainReasons.getReport_reasons().size() <= 0) {
            PToast.showShort("感谢您的反馈");
            dismiss();
            return;
        }
        LoadingDialog.show((FragmentActivity) App.activity, "提交中");
        RtcSubUtil.getInstance().saveReportShoot(response -> {
            String captureUrl = "";
            if (response.isOk()) {
                UpLoadResult uploadResult = (UpLoadResult) response.getBaseData();
                if (uploadResult != null)
                    captureUrl = uploadResult.getUploadUrl();
            }
            ModuleMgr.getCommonMgr().reqAddComplaint(complainReasons.getReport_reasons().get(selectedPosition).getReason(), captureUrl, null);
        });

        MsgMgr.getInstance().delay(this, () -> {
            LoadingDialog.closeLoadingDialog();
            PToast.showShort(App.activity.getString(R.string.chat_video_complain_suc));
            dismiss();
        }, 2500);
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.getUrlParam() == UrlParam.reqGetComplainReasons) {
            complainReasons = (RtcComplainReasons) response.getBaseData();
            reportAdapter.setList(complainReasons.getReport_reasons());
        }
    }

    private class ItemSpaces extends RecyclerView.ItemDecoration {

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            outRect.bottom = 20;
        }
    }
}
